//
//  StretchButtonView.swift
//  Yolan123
//
//  Created by Fenly on 2018/5/2.
//  Copyright © 2018年 Cotte. All rights reserved.
//

import UIKit
import pop

class StretchButtonView: UIView {

    @IBOutlet weak var whiteAreaView: UIImageView!
    // 显示的文字
    @IBOutlet weak var characterLabel: UILabel!
    // 外侧ImageView数组
    var roundImageArray: [UIImageView?] = []
    
    private let roundImageName = ["上二黄", "右黑", "右黄", "下黑", "下左黄","左一黄","左一黑", "上一黄"]
    // 开始图片偏移的(x, y)
    private let originalCoord = [(25, -50), (42, -13), (44, 28), (11, 52), (-29, 40), (-65, 16), (-45, -24), (-10, -50)]
    // 图片结束偏移的(x, y)
    private let stretchAngleArray = [(20, -30),(50, -22),(25, 20),(5, 40),(-26, 48),(-45, 5),(-53, -28),(-10, -30)]
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        // 添加八个方向图片
        for (index,name) in roundImageName.enumerated() {
            let roundImageView = UIImageView.init(image: UIImage.init(named: name))
            roundImageView.frame = CGRect(x: 0, y: 0, width: whiteAreaView.frame.size.width*0.35, height: whiteAreaView.frame.size.width*0.35*1.29)
            roundImageView.tag = 1000+index
            
            if index == 0||index == 1||index == 3||index == 5||index == 7{
                self.insertSubview(roundImageView, at: 0)
            } else {
                self.insertSubview(roundImageView, belowSubview: whiteAreaView)
            }
            roundImageView.snp.makeConstraints { (make) in
                make.center.equalToSuperview()
            }
            roundImageArray.append(roundImageView)
//            let angle = CGFloat.pi / 4 * CGFloat(index)
//            var transform = CGAffineTransform(rotationAngle: angle)
//            transform = transform.translatedBy(x: 0, y: -50)
//            roundImageView.transform = transform
        }
    
        let angleNum = Double.pi / 4

        // 围绕z轴旋转一定的角度
        for (index, roundImageView) in roundImageArray.enumerated() {
            // 1.3.6   2.5.7   4.8
            roundImageView?.addBasicAnimation(with: .rotation,
                                              duration: 0,
                                              fromValue: 0,
                                              toValue: angleNum*Double(index),
                                              repeatForever: false)
                        .addBasicAnimation(with: .translationXY, duration: 0, fromValue: CGSize(width: 0, height: 0), toValue: CGSize(width: CGFloat(originalCoord[index].0), height: CGFloat(originalCoord[index].1)), repeatForever: false)

        }
    }
    
    func setStretchButtonView(eightDirection: Bool) {
        if !eightDirection {
            // 仅显示四个方向
            for view in self.subviews {
                if view.tag == 1000||view.tag == 1002||view.tag == 1004||view.tag == 1006  {
                    view.removeFromSuperview()
                }
            }
        }
        
        startAnimation()
    }
    
    private func startAnimation() {
        for (index, roundImageView) in roundImageArray.enumerated() {
        var duration: TimeInterval = 0.0
        switch index {
        case 0,2,5:
            duration = 0.8
        case 1,4,6:
            duration = 0.8
        case 3,7:
            duration = 1
        default:
            break
        }
        roundImageView?.addBasicAnimation(with: .translationXY,
                                          duration: duration,
                                          fromValue: CGSize(width: CGFloat(originalCoord[index].0), height: CGFloat(originalCoord[index].1)),
                                          toValue: CGSize(width: stretchAngleArray[index].0, height: stretchAngleArray[index].1),
                                          repeatForever: true,
                                          autoreverses:true,
                                          completionBlock: { (_, _) in
            })


    }
    }
}
